#include "loopdrawablebuilder.h"
#include "db/dbconnection.h"

LoopDrawableBuilder::LoopDrawableBuilder()
{
}

void LoopDrawableBuilder::_createDrawableFromRecord(QSqlRecord record, QMap<QString, Drawable *> &map)
{
    QVariant x = record.value("centerX");
    QVariant y = record.value("centerY");
    QVariant size = record.value("size");
    QVariant form = record.value("form");
    Drawable *loop;
    if(form.toString() == "square")
    {
        loop  = new SquareLoopDrawable(QPointF(x.toDouble(), y.toDouble()), size.toDouble());
    }
    else
    {
        loop = new CircleLoopDrawable(QPointF(x.toDouble(), y.toDouble()), size.toDouble());
    }
    loop->setTag(this->_globalId);
    map.insert(record.value("idGeneratorLoop").toString(), loop);
}


QMap<QString, Drawable *> LoopDrawableBuilder::build()
{
    QMap<QString, Drawable *> map;

    if(!_model.isNull())
    {      
        _model->setFilter(_modelFilter);
        _model->select();
        for(int i=0; i<_model->rowCount(); i++)
        {
            QSqlRecord rec = _model->record(i);
            _createDrawableFromRecord(rec, map);
        }
    }
    else
    {
        _createDrawableFromRecord(_rowRecord[0], map);
    }
    return map;
}
